package com.google.code.seditor;

import com.google.code.seditor.db.DaoFactory;
import com.google.code.seditor.gui.MainEditor;
import com.google.code.seditor.gui.model.MainModel;
import com.google.code.seditor.utils.Constants;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class SeditorActivity extends ListActivity {

	private static final int INSERT_ID = Menu.FIRST;
	private static final int EXIT_ID = Menu.FIRST + 1;
	private static final int DELETE_ID = Menu.FIRST + 2;
	//private static final int EDIT_ID = Menu.FIRST + 3;

	private MainModel model = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		if(!this.checkSdCard()){
			this.finish();
			return;
		}
		
		super.onCreate(savedInstanceState);
		DaoFactory.init();
		this.model=new MainModel(this);
		setContentView(R.layout.main);
		this.fillData();
		registerForContextMenu(getListView());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, INSERT_ID, 0, R.string.menu_insert);
		menu.add(0, EXIT_ID, 0, R.string.menu_exit);
		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case INSERT_ID:
			createNote();
			return true;
		case EXIT_ID:
			this.finish();
			return true;
		}

		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    super.onCreateContextMenu(menu, v, menuInfo);
	    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
	    switch(item.getItemId()) {
	    case DELETE_ID:
	        deleteNote(item);
	        return true;
	    }
	    return super.onContextItemSelected(item);
	}
	
	@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        editNote(l,v,position,id);
    }
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }

	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		DaoFactory.close(this);
	}

	private void createNote(){
		Intent i = new Intent(this, MainEditor.class);
		startActivityForResult(i, Constants.NOTE_CREATE);
	}
		
	private void deleteNote(MenuItem item){
		this.model.deleteNote(((AdapterContextMenuInfo) item.getMenuInfo()).id);
	}
	
	private void editNote(ListView l, View v, int position, long id){
		Intent i = new Intent(this, MainEditor.class);
		i.putExtra(Constants.PARAM_ROWID, id);
		
		startActivityForResult(i, Constants.NOTE_EDIT);
	}
	
	private void fillData() {

		 // Get all of the notes from the database and create the item list
        Cursor c = this.model.getAllNote();
        startManagingCursor(c);
        
		String[] from = new String[] { MainModel.KEY_TITLE };
        int[] to = new int[] { R.id.text1 };
        
		SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, R.layout.note_row, c, from, to);
        setListAdapter(notes);
        c.close();
        
	}
	
	private boolean checkSdCard(){

		boolean mExternalStorageWriteable = false;
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
		    // We can read and write the media
		    mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
		    // We can only read the media
		    mExternalStorageWriteable = false;
		} else {
		    // Something else is wrong. It may be one of many other states, but all we need
		    //  to know is we can neither read nor write
		    mExternalStorageWriteable = false;
		}
		
		return mExternalStorageWriteable;
	}
}